package com.alinesno.cloud.portal.auto.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alinesno.cloud.common.core.constants.SpringInstanceScope;
import com.alinesno.cloud.common.facade.pageable.DatatablesPageBean;
import com.alinesno.cloud.common.facade.response.ResponseBean;
import com.alinesno.cloud.common.web.base.advice.TranslateCode;
import com.alinesno.cloud.common.web.base.controller.FeignMethodController;
import com.alinesno.cloud.portal.auto.constants.CommonPath;
import com.alinesno.cloud.portal.auto.entity.AutoTaskEntity;
import com.alinesno.cloud.portal.auto.service.IAutoTaskService;

/**
 * <p> 前端控制器 </p>
 *
 * @author WeiXiaoJin
 * @since 2019-11-21 07:41:47
 */
@Controller
@Scope(SpringInstanceScope.PROTOTYPE)
@RequestMapping("portal/auto/autoTask")
public class AutoTaskController extends FeignMethodController<AutoTaskEntity, IAutoTaskService> {
	//日志记录
	private static final Logger log = LoggerFactory.getLogger(AutoTaskController.class);

	@Autowired
	private IAutoTaskService autoTaskService;
	
	
	@TranslateCode(value="[{hasStatus:has_status}]")
	@ResponseBody
	@PostMapping("/datatables")
    public DatatablesPageBean datatables(HttpServletRequest request , Model model ,  DatatablesPageBean page){
		log.debug("page = {}" , ToStringBuilder.reflectionToString(page));
		return this.toPage(model, this.getFeign() , page) ;
    }

	@Override
	public IAutoTaskService getFeign() {
		return this.autoTaskService;
	}

	/**
	 * 运行任务
	 * @return
	 */
	@ResponseBody
	@GetMapping("/runTask")
	public ResponseBean runTask(@NotBlank(message = "主键不能为空.") String id) {
		log.debug("task id : {}");
		autoTaskService.runGatlingTask(id) ; 
		return ok() ; 
	}

	/** 
	 * 打开报告 
	 * @throws IOException 
	 */
	@GetMapping("/report")
	public String report(@NotBlank(message = "主键不能为空.") String id) throws IOException {
		log.debug("task id : {}");

		AutoTaskEntity e = autoTaskService.findById(id).get() ; 
		String reportPath = CommonPath.getReportPath(e) ; 
		
		log.debug("report path:{}" , reportPath);
		
		return redirect(reportPath) ; 
	}
	
	/**
	 * 保存代码
	 * @return
	 */
	@ResponseBody
	@PostMapping("/code")
	public ResponseBean code(String code) {
		log.debug(code);
		return ok() ; 
	}

	@Override
	public void modify(HttpServletRequest request, Model model, String id) {
		Assert.hasLength(id , "主键不能为空.");
		AutoTaskEntity code = autoTaskService.getOne(id) ;
		
		code.setScriptContent(code.getScriptContent().replaceAll("\\|", "\r\n"));
		log.debug("modify bean:{}" , ToStringBuilder.reflectionToString(code));
		
		model.addAttribute("bean", code) ; 
	}
	
}


























